package com.atguigu.eduorder.controller;


import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.CourseWebVoOrder;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.eduorder.client.EduClient;
import com.atguigu.eduorder.client.UcenterClient;
import com.atguigu.eduorder.entity.Like;
import com.atguigu.eduorder.service.HistoryService;
import com.atguigu.eduorder.service.LikeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gengky
 * @since 2022-04-15
 */
@RestController
@RequestMapping("/eduorder/like")
public class LikeController {

    @Autowired
    LikeService likeService;

    @Autowired
    HistoryService historyService;

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    /**
     * 取消收藏课程
     * @param courseId
     * @param memberId
     * @return
     */
    @PostMapping("notLikeCourse/{courseId}/{memberId}")
    public R notLikeCourse(@PathVariable("courseId") String courseId, @PathVariable("memberId") String memberId) {

        QueryWrapper<Like> wrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseId)){
            wrapper.eq("course_id",courseId);
        }
        if (!StringUtils.isEmpty(memberId)){
            wrapper.eq("user_id",memberId);
        }
        likeService.remove(wrapper);

        return R.ok();
    }

    /**
     * 收藏课程
     * @param courseId
     * @param memberId
     * @return
     */
    @PostMapping("likeCourse/{courseId}/{memberId}")
    public R likeCourse(@PathVariable("courseId") String courseId, @PathVariable("memberId") String memberId) {

        //通过远程调用 根据用户ID 获取用户信息，调用 service-ucenter 的 UcenterMemberController
        UcenterMemberOrder member = ucenterClient.getUserInfoOrder(memberId);
        //通过远程调用 根据课程ID 获取课程信息，
        CourseWebVoOrder course = eduClient.getCourseInfoOrder(courseId);

        Like like=new Like();
        like.setCourseId(courseId);
        like.setCourseCover(course.getCover());
        like.setCourseTitle(course.getTitle());
        like.setUserId(memberId);
        like.setNickname(member.getNickname());
        like.setGmtCreate(new Date());
        like.setGmtModified(new Date());

        likeService.save(like);

        return R.ok();
    }

    /**
     * 根据课程ID 和 会员ID 查询订单表中改课程是否 收藏
     * @return
     */
    @GetMapping("isLikeCourse/{courseId}/{memberId}")
    public R isLike(@PathVariable("courseId") String courseId, @PathVariable("memberId") String memberId) {
        System.out.println("---------------");
        System.out.println("调用了 isLike");
        QueryWrapper<Like> wrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseId)){
            wrapper.eq("course_id",courseId);
        }
        if (!StringUtils.isEmpty(memberId)){
            wrapper.eq("user_id",memberId);
        }
        int count = likeService.count(wrapper);

        System.out.println("count: "+count);

        if (count>0){
            return R.ok().data("isLike",true);
        }else {
            return R.ok().data("isLike",false);
        }
    }

    @ApiOperation("根据会员ID查询收藏的课程")
    @GetMapping("getLikeByNumberId/{memberid}")
    public R getLikeByNumberId(@PathVariable String memberid){
        QueryWrapper<Like> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",memberid);
//        queryWrapper.last("limit 4");
        List<Like> list = likeService.list(queryWrapper);
        return R.ok().data("likeCourses",list);
    }


    @GetMapping("getLikeListByNumberId/{memberid}")
    public List<Like> getLikeListByNumberId(@PathVariable String memberid){
        QueryWrapper<Like> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",memberid);
        List<Like> list = likeService.list(queryWrapper);
        return list;
    }


}

